package com.zhxi.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhxi.common.utils.PageUtils;
import com.zhxi.common.utils.Query;
import com.zhxi.common.utils.R;
import com.zhxi.gulimall.product.dao.CategoryDao;
import com.zhxi.gulimall.product.entity.CategoryEntity;
import com.zhxi.gulimall.product.service.CategoryBrandRelationService;
import com.zhxi.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品三级分类
 *
 * @Author: zhxi
 * @Email: 664480310@qq.com
 * @Date: 2024-08-19 17:41:15
 * @Version: 1.0.0
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    @Autowired
    private CategoryBrandRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取所有分类（树形结构）
     */
    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> list = baseMapper.selectList(null);
        List<CategoryEntity> collect = list.stream().filter((item) -> item.getParentCid() == (0)).map((item -> {
            item.setChildren(findChildren(item, list));
            return item;
        })).collect(Collectors.toList());
        return collect;
    }

    /**
     * 批量删除
     *
     * @param list ids数组
     * @return 删除结果
     */
    @Override
    public R removeMenu(List<Long> list) {
        //todo 删除前检查
        baseMapper.deleteByIds(list);
        return R.ok();
    }

    @Override
    public Long[] findCatelogIds(Long cateId) {
        List<Long> path = new ArrayList<>();
        List<Long> paths = findParentId(cateId, path);
        Collections.reverse(paths);
        return paths.toArray(new Long[0]);
    }

    @Override
    public void updateCascade(CategoryEntity category) {
        updateById(category);
        relationService.updateCategory(category.getCatId(), category.getName());
    }

    private List<Long> findParentId(Long cateId, List<Long> path) {
        path.add(cateId);
        CategoryEntity category = this.getById(cateId);
        if (category.getParentCid() != 0) {
            findParentId(category.getParentCid(), path);
        }
        return path;
    }

    /**
     * 递归封装树结构
     *
     * @param category 当前分类
     * @param list     所有分类
     * @return 子分类
     */
    private List<CategoryEntity> findChildren(CategoryEntity category, List<CategoryEntity> list) {
        List<CategoryEntity> collect = list.stream().filter(item -> {
            return category.getCatId().equals(item.getParentCid());
        }).map(item -> {
            item.setChildren(findChildren(item, list));
            return item;
        }).collect(Collectors.toList());
        return collect;
    }
}